package com.itclj.common.util;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * 读取配置
 * 
 * @author lujun.chen
 * @version 2016年11月15日
 * @since JDK1.8
 */
public class PropertyUtil {
	private static final String[] PATHS = { "/config/config.properties","/config/redis.properties" };

	private static Properties props = null;

	private static long start = 0L;

	/**
	 * @return void [返回类型说明]
	 * @exception throws
	 *                [违例类型] [违例说明]
	 * @see [类、类#方法、类#成员]
	 */
	private static void init() {
		long end = System.currentTimeMillis();
		// 为空时或加载超过1小时
		if (props == null || end - start >= 3600000) {
			start = end;

			InputStream in = null;
			props = new Properties();
			try {
				for (String PATH : PATHS) {
					in = PropertyUtil.class.getResourceAsStream(PATH);
					props.load(in);
				}
			} catch (IOException e) {
				e.printStackTrace();
			} finally {
				if (in != null) {
					try {
						in.close();
					} catch (IOException ex) {
						ex.printStackTrace();
					}
				}
			}
		}
	}

	/**
	 * 获取属性文件值
	 * 
	 * @param key
	 * @return
	 */
	public static String getValue(String key) {
		init();
		return props.getProperty(key);
	}
}
